<?php

namespace app\models\Child;

use Yii;

/**
 * This is the model class for table "t_order_info".
 *
 * @property integer $order_id
 * @property string $booking_no
 * @property string $order_no
 * @property integer $cashier_id
 * @property integer $user_id
 * @property integer $doctor_id
 * @property integer $subclinic_id
 * @property integer $patient_id
 * @property integer $coupon_id
 * @property string $order_total_cost
 * @property string $order_pay_cost
 * @property string $coupon_total_cost
 * @property integer $pay_way
 * @property integer $order_state
 * @property integer $pay_state
 * @property string $order_time
 */
class TOrderInfo extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 't_order_info';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['cashier_id', 'booking_id', 'user_id', 'doctor_id', 'subclinic_id', 'patient_id', 'coupon_id', 'pay_way', 'order_state', 'pay_state','is_reply'], 'integer'],
            [['order_total_cost', 'order_pay_cost', 'coupon_total_cost'], 'number'],
            [['order_time'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'order_id' => 'Order ID',
            'booking_id' => 'Booking ID',
            'order_no' => 'Order No',
            'cashier_id' => 'Cashier ID',
            'user_id' => 'User ID',
            'doctor_id' => 'Doctor ID',
            'subclinic_id' => 'Subclinic ID',
            'patient_id' => 'Patient ID',
            'coupon_id' => 'Coupon ID',
            'order_total_cost' => 'Order Total Cost',
            'order_pay_cost' => 'Order Pay Cost',
            'coupon_total_cost' => 'Coupon Total Cost',
            'pay_way' => 'Pay Way',
            'order_state' => 'Order State',
            'pay_state' => 'Pay State',
            'order_time' => 'Order Time',
            'is_reply' => 'Is Reply',
        ];
    }
//    /*
//     * 获取订单列表
//     */
//    public function getOrderList($user_id)
//    {
//        $list = $this->find()
//                ->select('t_order_info.*,t_patient_info.*,t_subclinic_info.*,sys_user_info.*,t_booking_info.*')
//                ->join('left join','t_patient_info','t_patient_info.patient_id = t_order_info.patient_id')
//                ->join('left join','t_subclinic_info','t_subclinic_info.subclinic_id = t_order_info.subclinic_id')
//                ->join('left join','sys_user_info','sys_user_info.user_id = t_order_info.doctor_id')
//                ->join('left join','t_booking_info','t_booking_info.booking_id = t_order_info.booking_id')
//                ->where(['t_order_info.patient_id' => $user_id])
//                ->asArray()
//                ->all();
//        return $list;
//    }
    
    /*
     * 获取订单列表
     */
    public function getOrderList($user_id)
    {
        $list = $this->find()
                ->select('t_order_info.order_id,order_no,subclinic_name,sys_user_info.user_name,booking_time,order_state')
                ->join('left join','t_subclinic_info','t_subclinic_info.subclinic_id = t_order_info.subclinic_id')
                ->join('left join','sys_user_info','sys_user_info.user_id = t_order_info.doctor_id')
                ->join('left join','t_booking_info','t_booking_info.booking_id = t_order_info.booking_id')
                ->where(['t_order_info.user_id' => $user_id])
                ->asArray()
                ->all();
        return $list;
    }
    /**根据分店id查询订单数
     * @param $subclinic_id
     */
    public function getOrderBySubclinicIdCount($subclinic_id)
    {
        $list = $this->find()
            ->select('t_order_info.order_id,order_no,subclinic_name,sys_user_info.user_name,booking_time,order_state')
            ->join('left join','t_subclinic_info','t_subclinic_info.subclinic_id = t_order_info.subclinic_id')
            ->join('left join','sys_user_info','sys_user_info.user_id = t_order_info.doctor_id')
            ->join('left join','t_booking_info','t_booking_info.booking_id = t_order_info.booking_id')
            ->where(['t_order_info.subclinic_id' => $subclinic_id])
            ->asArray()
            ->count();
        return $list;
    }
    /*
     * 获取订单详情
     */
    public function getOrderInfo($booking_id)
    {
        $list = $this->find()
                ->select('t_order_info.order_id,order_state,order_no,booking_time,sys_user_info.user_name as doctor_name,order_time,t_user_info.user_img,t_user_info.user_name,t_user_info.user_phone,grade_name,grade_img,sys_discount_card_info.card_name as discount_name,b.type_name as discount_type,sys_account_card_info.card_name as account_name,a.type_name as account_type,order_total_cost,order_pay_cost,coupon_total_cost')
                ->join('left join','t_booking_info','t_booking_info.booking_id = t_order_info.booking_id')
                ->join('left join','sys_user_info','sys_user_info.user_id = t_order_info.doctor_id')
                ->join('left join','t_user_info','t_user_info.user_id = t_order_info.user_id')
                ->join('left join','init_grade_user','init_grade_user.grade_id = t_user_info.user_level')
                ->join('left join','sys_account_card_info','sys_account_card_info.card_id = t_order_info.card_id')
                ->join('left join','sys_discount_card_info','sys_discount_card_info.card_id = t_order_info.coupon_id')
                ->join('left join','sys_discount_card_type a','a.type_id = sys_account_card_info.card_type')
                ->join('left join','sys_discount_card_type b','b.type_id = sys_discount_card_info.card_type')
                ->where(['t_order_info.booking_id'=>$booking_id])
                ->asArray()
                ->one();
        return $list;
    }
    
    /*
     * 获取所有医生的已完成的订单量
     */
    public function getSimple()
    {
        $simples = $this->find()
                ->select('doctor_id,count(order_id) as simple')
                ->where(['order_state' => 0])
                ->groupBy('doctor_id')
                ->asArray()
                ->all();
        return $simples;
    }
    
    /*
     * 获取需要评价的订单信息
     */
    public function orderList($booking_id)
    {
        $list = $this->find()
                ->select('booking_id,order_id,t_user_info.user_id,t_user_info.user_name,sys_user_info.user_id as doctor_id,sys_user_info.user_name as doctor_name,sys_user_info.user_img as doctor_img')
                ->join('left join','sys_user_info','sys_user_info.user_id = t_order_info.doctor_id')
                ->join('left join','t_user_info','t_user_info.user_id = t_order_info.user_id')
                ->where(['booking_id' => $booking_id])
                ->asArray()
                ->one();
        return $list;
    }
    
    /*
     * 通过预约id获取订单id
     */
    public function getOrderId($booking_id)
    {
        $list = $this->find()
                ->where(['booking_id' => $booking_id])
                ->asArray()
                ->one();
        return $list;
    }
    
    /*
     * 修改评价状态
     */
    public function setStatus($order_id)
    {
        $order = $this->find()
                ->where(['order_id' => $order_id])
                ->one();
        $order->is_reply = 1;
        
        return $order->save();
    }
    
}
